<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <article-item
        v-for="obj in searchArticleList"
        :key="obj.art_id"
        :artObj="obj"
        @click.native="toArticleDetailFn(obj.art_id)"
      ></article-item>
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem'

import { getSearchArticleListAPI } from '@/api'
export default {
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  data () {
    return {
      loading: false,
      finished: false,
      page: 1,
      searchArticleList: []
    }
  },
  methods: {
    onLoad () {
      this.page++
      this.getSearchArticleListFn()
    },
    async getSearchArticleListFn () {
      const res = await getSearchArticleListAPI({
        artId: this.$route.params.keyword,
        page: this.page
      })
      if (res.data.data.results === null || res.data.data.results.length === 0) {
        this.finished = true
      }
      this.searchArticleList = [...this.searchArticleList, ...res.data.data.results]
      //   console.log(this.searchArticleList)
      this.loading = false
    },
    toArticleDetailFn (artId) {
      this.$router.push({
        path: '/article',
        query: {
          aId: artId
        }
      })
    }
  },

  created () {
    // console.log(this.$route.params.keyword)
    this.getSearchArticleListFn()
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
/deep/ .van-icon-arrow-left {
  color: white;
}
</style>
